package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.order.IPointsLogService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.order.PointsLogDetailVo;
import com.mdd.admin.vo.order.PointsLogListVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.user.PointsLog;
import com.mdd.common.enums.PointsChangeEnum;
import com.mdd.common.mapper.user.PointsLogMapper;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 用户账户记录实现类
 * @author panweiliang
 */
@Service
public class PointsLogServiceImpl implements IPointsLogService {
        
    @Resource
    PointsLogMapper pointsLogMapper;

    /**
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<PointsLogListVo>
     */
    @Override
    public PageResult<PointsLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        MPJQueryWrapper<PointsLog> mpjQueryWrapper = new MPJQueryWrapper<PointsLog>()
                .selectAll(PointsLog.class)
                .select("user.sn as userSn, user.nickname as nickname, user.id as userId, user.avatar as avatar, user.mobile as mobile ")
                .innerJoin("?_user user ON user.id=t.user_id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .orderByDesc(Arrays.asList("t.id"));

        if (StringUtil.isNotEmpty(params.get("keyword"))) {
            String keyword = params.get("keyword");
            mpjQueryWrapper.nested(wq->wq
                    .like("user.sn", keyword).or()
                    .like("user.nickname", keyword).or()
                    .like("user.mobile", keyword));
        }

        pointsLogMapper.setSearch(mpjQueryWrapper, params, new String[]{
                "=:changeType@t.change_type:int",
                "datetime:startTime-endTime@t.create_time:str",
        });


        IPage<PointsLogListVo> iPage = pointsLogMapper.selectJoinPage(new Page<>(page, limit), PointsLogListVo.class, mpjQueryWrapper);

        for(PointsLogListVo item : iPage.getRecords()) {
            item.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            item.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            item.setAssociationSn(item.getAssociationSn() != null ? item.getAssociationSn() : "-");
            item.setAvatar(UrlUtil.toAbsoluteUrl(item.getAvatar()));
            item.setChageTypeStr(changeTypeMsg(item.getChangeType()));
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }


    private String changeTypeMsg(int changeType) {
        if (changeType == PointsChangeEnum.BNW_DEC_ADMIN.getCode()) {
            return PointsChangeEnum.BNW_DEC_ADMIN.getMsg();
        } else if (changeType == PointsChangeEnum.BNW_DEC_ORDER.getCode()) {
            return PointsChangeEnum.BNW_DEC_ORDER.getMsg();
        } else if (changeType == PointsChangeEnum.BNW_INC_ADMIN.getCode()) {
            return PointsChangeEnum.BNW_INC_ADMIN.getMsg();
        } else if (changeType == PointsChangeEnum.BNW_INC_ORDER_CANCEL.getCode()) {
            return PointsChangeEnum.BNW_INC_ORDER_CANCEL.getMsg();
        } else if (changeType == PointsChangeEnum.BNW_INC_ORDER.getCode()) {
            return PointsChangeEnum.BNW_INC_ORDER.getMsg();
        } else if (changeType == PointsChangeEnum.BNW_DEC_ORDER_CANCEL.getCode()) {
            return PointsChangeEnum.BNW_DEC_ORDER_CANCEL.getMsg();
        } else {
            return PointsChangeEnum.BNW_INC_SIGIN.getMsg();
        }
    }

    /**
     * 用户账户记录详情
     *
     * @author panweiliang
     * @param id 主键参数
     * @return PointsLogDetailVo
     */
    @Override
    public PointsLogDetailVo detail(Integer id) {
        PointsLog model = pointsLogMapper.selectOne(
                new QueryWrapper<PointsLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        PointsLogDetailVo vo = new PointsLogDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户账户记录删除
     *
     * @author panweiliang
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        PointsLog model = pointsLogMapper.selectOne(
                new QueryWrapper<PointsLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        pointsLogMapper.delete(new QueryWrapper<PointsLog>().eq("id", id));
    }

}
